package com.wanmait.recruit.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.mapper.CityMapper;
import com.wanmait.recruit.mapper.EducationalBackgroundTypeMapper;
import com.wanmait.recruit.mapper.PositionAttrMapper;
import com.wanmait.recruit.mapper.PositionTypeMapper;
import com.wanmait.recruit.pojo.*;
import com.wanmait.recruit.service.*;
import com.wanmait.recruit.util.PageContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/web/company/manage/position")
public class CompanyPositionController {

    @Resource
    IndustryService industryService;

    @Resource
    PositionAttrService positionAttrService;

    @Resource
    WorkTimeService workTimeService;

    @Resource
    PositionTypeMapper positionTypeMapper;

    @Resource
    CompanyPositionService companyPositionService;

    @Resource
    CompanyHrService companyHrService;

    @Resource
    CompanyAdminService companyAdminService;

    @Resource
    CityMapper cityMapper;



    @PostMapping("/update")
    public ModelAndView update(CompanyPosition companyPosition,String cityName){
        City city = cityMapper.findByCityName(cityName);
        companyPosition.setCityId(city.getId());
        companyPositionService.updatePosition(companyPosition);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/web/company/manage/position/update");
        mv.addObject("msg","success");
        return mv;
    }

    @GetMapping("/update")
    public ModelAndView update(Integer id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/company/manage/position/update");

        CompanyPosition companyPosition = companyPositionService.findById(id);


        List<Industry> industryList = industryService.findAll();
        mv.addObject(industryList);
        List<PositionAttr> positionAttrs = positionAttrService.findAll();
        mv.addObject(positionAttrs);
        List<WorkTime> workTimes = workTimeService.findAll();
        mv.addObject(workTimes);
        List<EducationalBackgroundType> educationalBackgroundTypes = educationalBackgroundTypeMapper.findAll();
        mv.addObject(educationalBackgroundTypes);

        mv.addObject("companyPosition",companyPosition);
        return mv;
    }

    @RequestMapping("/updateEnable")
    public void updateEnable(Integer id){
        companyPositionService.updateEnable(id);
    }

    @RequestMapping("/positionListData")
    public ModelAndView positionListData(HttpSession session){

        ModelAndView mv= new ModelAndView();
        mv.setViewName("/web/company/manage/position/positionListData");
        Person person = (Person)session.getAttribute("person");
        CompanyHr companyHr = companyHrService.findByPerson(person);
        if(companyHr!=null){
            Company company = companyHr.getCompany();
            PageHelper.startPage(PageContext.getPageNum(),2);
            List<CompanyPosition> companyPositions = companyPositionService.findByHr(person,company);
            PageInfo<CompanyPosition> pageInfo = new PageInfo<>(companyPositions);
            mv.addObject("companyPositions",companyPositions);
            mv.addObject(pageInfo);
            return mv;
        }
        CompanyAdmin companyAdmin = companyAdminService.findByPerson(person);
        if(companyAdmin!=null){
            Company company = companyAdmin.getCompany();
            PageHelper.startPage(PageContext.getPageNum(),2);
            List<CompanyPosition> companyPositions = companyPositionService.findByAdmin(company);
            PageInfo<CompanyPosition> pageInfo = new PageInfo<>(companyPositions);
            mv.addObject("companyPositions",companyPositions);
            mv.addObject(pageInfo);
            return mv;
        }

        return mv;
    }

    @RequestMapping("/positionList")
    public ModelAndView positionList(HttpSession session){

        ModelAndView mv= new ModelAndView();
        mv.setViewName("/web/company/manage/position/positionList");
        Person person = (Person)session.getAttribute("person");
        CompanyHr companyHr = companyHrService.findByPerson(person);
        if(companyHr!=null){
            Company company = companyHr.getCompany();
            PageHelper.startPage(PageContext.getPageNum(),2);
            List<CompanyPosition> companyPositions = companyPositionService.findByHr(person,company);
            PageInfo<CompanyPosition> pageInfo = new PageInfo<>(companyPositions);
            mv.addObject("companyPositions",companyPositions);
            mv.addObject(pageInfo);
            return mv;
        }
        CompanyAdmin companyAdmin = companyAdminService.findByPerson(person);
        if(companyAdmin!=null){
            Company company = companyAdmin.getCompany();
            PageHelper.startPage(PageContext.getPageNum(),2);
            List<CompanyPosition> companyPositions = companyPositionService.findByAdmin(company);
            PageInfo<CompanyPosition> pageInfo = new PageInfo<>(companyPositions);
            mv.addObject("companyPositions",companyPositions);
            mv.addObject(pageInfo);
            return mv;
        }

        return mv;
    }

    @RequestMapping("/checkCity")
    @ResponseBody
    public void checkCity(String cityName,HttpServletResponse response) throws IOException {
        City city = null;
        try {
            city = cityMapper.findByCityName(cityName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PrintWriter out = response.getWriter();
        if(city==null){
            out.print(0);
        }
        else{
            out.print(1);
        }
        out.close();

    }

    @PostMapping("/addPosition")
    public ModelAndView addPosition(CompanyPosition companyPosition,String cityName, HttpSession session,HttpServletResponse response) throws IOException {

        Person person = (Person)session.getAttribute("person");
        CompanyHr companyHr = companyHrService.findByPerson(person);
        if(companyHr!=null){
            companyPosition.setCompanyId(companyHr.getCompanyId());
        }
        else {
            CompanyAdmin companyAdmin = companyAdminService.findByPerson(person);
            companyPosition.setCompanyId(companyAdmin.getCompanyId());
        }
        companyPosition.setPersonId(person.getId());
        //System.out.println(companyPosition.toString());
        City city = cityMapper.findByCityName(cityName);
        companyPosition.setCityId(city.getId());
        companyPositionService.insert(companyPosition);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/web/company/manage/position/addPosition");
        mv.addObject("msg","success");
        return mv;
    }


    @RequestMapping("/findPositionType")
    @ResponseBody
    public List<PositionType> findPositionType(Integer industryId, HttpServletResponse response) throws IOException {
        List<PositionType> positionTypes = positionTypeMapper.findByIndustryId(industryId);
        return positionTypes;
    }

    @Resource
    EducationalBackgroundTypeMapper educationalBackgroundTypeMapper;

    @GetMapping("/addPosition")
    public ModelAndView addPosition(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/company/manage/position/addPosition");
        List<Industry> industryList = industryService.findAll();
        mv.addObject(industryList);
        List<PositionAttr> positionAttrs = positionAttrService.findAll();
        mv.addObject(positionAttrs);
        List<WorkTime> workTimes = workTimeService.findAll();
        mv.addObject(workTimes);
        List<EducationalBackgroundType> educationalBackgroundTypes = educationalBackgroundTypeMapper.findAll();
        mv.addObject(educationalBackgroundTypes);
        return mv;
    }
}
